package com.ruoyi.mateproduction.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateProductionRecord;
import com.ruoyi.mateproduction.domain.MateProductionRecordModel;
import com.ruoyi.mateproduction.domain.MtCourseCheck;
import com.ruoyi.mateproduction.domain.MtCourseParameter;
import com.ruoyi.mateproduction.domain.MtCourseProject;
import com.ruoyi.mateproduction.domain.MtCourseSample;
import com.ruoyi.mateproduction.mapper.MateCourseMapper;
import com.ruoyi.mateproduction.service.MateCourseService;
import com.ruoyi.mateproduction.service.MtProductionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author
 * @Version 1.0 生产过程检验记录
 */
@RestController
@RequestMapping("/mateCourse")
public class MateCourseController extends BaseController {

    @Autowired
    MateCourseService mateCourseService;

    @Autowired
    MtProductionRecordService mtProductionRecordService;

    @Autowired
    MateCourseMapper mateCourseMapper;


    /**
     * 保存生产过程检验记录
     */
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping("/addMateCourse")
    public AjaxResult addMateCourse(@RequestBody MtCourseCheck mtCourseCheck) {
        mtCourseCheck.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mateCourseService.addMateCourse(mtCourseCheck);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @GetMapping("/createWord")
    public AjaxResult createWord(@RequestParam(value = "mtCourseId", required = false) Long mtCourseId) {
        String message = mateCourseService.createWord(mtCourseId);
        if (message.equals("生成失败")) {
            return AjaxResult.error("生成Word文件失败");
        } else {
            return AjaxResult.success(message);
        }
    }

    /**
     * 查询全部的生产过程检验记录
     */
    @GetMapping("/mateCourseList")
    @ResponseBody
    public TableDataInfo mateCourseList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        MtCourseCheck mtCourseCheck = new MtCourseCheck();  
        mtCourseCheck.setBeginTime(beginTime);
        mtCourseCheck.setEndTime(endTime);
        startPage();
        mtCourseCheck.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MtCourseCheck> list = mateCourseService.mateCourseList(mtCourseCheck);
        return getDataTable(list);
    }

    /*
   查询已经做完的生产过程检验记录
  */
    @GetMapping("/selectMateCourse")
    public Map<String, Object> selectMateCourse(@RequestParam(value = "mtCourseId", required = false) Long mtCourseId) {
        Map<String, Object> resMap = new HashMap<>();
        MtCourseCheck mtCourseCheck = mateCourseService.selectMateCourse(mtCourseId);
      /*  if (mtCourseCheck != null) {
            List<MtCourseProject> uniqueList = mtCourseCheck.getMtCourseProjects().stream()
                    .collect(Collectors.toMap(info -> info.getProcessName() + "|" + info.getCheckProject(), info -> info, (existing, replacement) -> existing))
                    .values().stream()
                    .collect(Collectors.toList());
            Collections.sort(uniqueList, (info1, info2) -> Math.toIntExact(info1.getMtProjectId() - info2.getMtProjectId()));
            mtCourseCheck.setMtCourseProjects(uniqueList);
        }*/

        resMap.put("mtCourseCheck", mtCourseCheck);
        return resMap;
    }

    /**
     * 删除生产过程检验记录
     */
    @Log(title = "删除生产过程检验记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{mtCourseId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("mtCourseId") Long mtCourseId) {
        mateCourseService.remove(mtCourseId);
        return AjaxResult.success("删除成功");
    }

    /*
      查询已经审核通过的生产批记录
       */
    @GetMapping("/auditRecord")
    public Map<String, Object> auditRecord() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateProductionRecord> mateProductionRecords = mtProductionRecordService.auditRecord(SecurityUtils.getDeptId());
        resMap.put("result", mateProductionRecords);
        return resMap;
    }

    /*
     查询已经审核通过的生产批记录
      */
    @GetMapping("/auditCourse")
    public Map<String, Object> auditCourse() {
        Map<String, Object> resMap = new HashMap<>();
        List<MtCourseCheck> mtCourseChecks = mateCourseService.auditCourse(SecurityUtils.getDeptId());
        resMap.put("result", mtCourseChecks);
        return resMap;
    }

    /*
      编辑生产过程检验记录
         */
    @PostMapping("/updateCourse")
    @ResponseBody
    public AjaxResult updateCourse(@RequestBody MtCourseCheck mtCourseCheck) {
        int num = mateCourseService.updateCourse(mtCourseCheck);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }
}

